layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;

	var table_filter = 'qingkuan_list',
		form_filter = 'qingkuan_search_form';
	var where_json = {
		_o: 'id',
		_b: 'desc'
	};
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: qingkuan_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'id',
			_b: 'desc'
		},
		cols: [qingkuan_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#qingkuanToolbar',
		defaultToolbar: [],
		initSort: {
			field: 'id',
			type: 'desc'
		}
	});

	form.render('select');

	$('#qingkuan_search_btn').on('click', function() {
		layer.load();
		//where_json.page = 1;
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
	})
	form.on('select', function(data) {
		var value = data.value;
		var e = data.elem;
		var id = $(e).attr('id');
		select_search(id, value);
	})

	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		select_search(name, value);
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});

	var pici = laydate.render({
		elem: 'input[name="pici"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search('pici', value);
		}
	});
	var pici_end = laydate.render({
		elem: 'input[name="pici_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search('pici_end', value);
		}
	});


	var select_search = function(field, value) {
		var where = {};
		where[field] = value;
		where_json[field] = value;
	};

	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch (obj.event) {
			case 'daochu':
				layer.confirm('确定导出本次查询吗？', {
					yes: function() {
						layer.load(2);
						send_info(export_xsl_url, where_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载excel',
								content: data.data
							});
						})
					}
				});
				break;
			default:
				break;
		}
		return false;
	})

	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var out_order_no = data.out_order_no;
		switch (e) {
			case 'viewDeff':
				layer.open({
					type: 2,
					anim: -1,
					title: '信息对比',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '80%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: view_deff_url + '?out_order_no=' + out_order_no
				})
				break;
			default:
				break;
		}
	})

	// 导入入库总表
	var upload_rukuzongbiao = upload.render({
		elem: '.rukuzongbiao',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(explodeRukuzongbiao_url + '?file=' + res.data.save_path, {}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.msg(data.msg, {
						icon: 1,
						anim: 6
					});
					table.reload(table_filter, where_json)
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 导入回款比例
	var upload_huikuanbili = upload.render({
		elem: '.huikuanbili',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(explodeHuikuanbili_url + '?file=' + res.data.save_path, {}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.msg(data.msg, {
						icon: 1,
						anim: 6
					});
					table.reload(table_filter, where_json)
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 导入请款状态
	var upload_qingkuanstate = upload.render({
		elem: '.qingkuanstate',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(explodeQingkuan_url + '?file=' + res.data.save_path, {}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.msg(data.msg, {
						icon: 1,
						anim: 6
					});
					table.reload(table_filter, where_json)
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});
})